﻿using System.Drawing;
using System.Drawing.Drawing2D;
using CanvasControl.Model;
using CanvasControl.View;
using GraphDrawing.Graph.Model;

namespace GraphDrawing.Graph.View
{
    public class ConnectionView
    {
        public LineAppearance Default { get; private set; }
        public LineAppearance Selected { get; private set; }

        public ConnectionView(LineAppearance @default, LineAppearance selected)
        {
            Default = @default;
            Selected = selected;
        }

        public void Draw(DrawingContext dc, ViewPortModel viewPort, ConnectionModel connection)
        {
            var rect1 = viewPort.GlobalToClient(connection.From.GetBounds());
            var rect2 = viewPort.GlobalToClient(connection.To.GetBounds());
            var p1 = new Point(rect1.X + rect1.Width/2, rect1.Y + rect1.Height/2);
            var p2 = new Point(rect2.X + rect2.Width/2, rect2.Y + rect2.Height/2);

            var cookie = dc.Graphics.BeginContainer();
            try
            {
                dc.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                dc.Graphics.CompositingQuality = CompositingQuality.HighQuality;
                var path = GraphicsPathFactory.CreatePath(connection.ShapeType, p1, p2);
                var appearance = connection.IsSelected ? Selected : Default;
                using (var pen = appearance.CreatePen())
                    dc.Graphics.DrawPath(pen, path);
            }
            finally
            {
                dc.Graphics.EndContainer(cookie);
            }
        }
    }
}